package com.controller;

import com.utils.QiniuUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class QiniuController {



    @RequestMapping(value = "/uploadQiniu",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public Map<String,Object> uploadQiniu(@RequestParam(required = false)MultipartFile[] ff) throws Exception{
        Map<String,Object> mp = new HashMap();
        System.out.println("-----------upload qinu");
       /* mp.put("title",title);
        mp.put("dtime",dtime);*/

        String fileurl = "";
        //文件处理
        if(ff !=null && ff.length>0){
            //循环
            for(MultipartFile mf:ff){
                String oname = mf.getOriginalFilename();
                int index = oname.lastIndexOf(".");
                if(index != -1){
                    String fname = UUID.randomUUID().toString().replace("-", "");
                    fname += oname.substring(index);
                    //
                    fileurl = QiniuUpload.updateFile(mf,fname); //上传七牛
                    //
                    mp.put("fileurl",fileurl);
                }
                //
            }
        }
        // 入数据库
        mp.put("addcode",1);
        //
        System.out.println("-----------upload qiniu mp = "+mp);
        //
        return mp;
    }
}
